package com.ruoyi.modularbusiness.service.impl;

import java.util.List;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.modularbusiness.domain.Formrecording;
import com.ruoyi.modularbusiness.domain.Formtype;
import com.ruoyi.modularbusiness.domain.VO.AubitTypeVo;
import com.ruoyi.modularbusiness.domain.VO.FormRtbVo;
import com.ruoyi.modularbusiness.mapper.FormrecordingMapper;
import com.ruoyi.modularbusiness.service.IFormrecordingService;
import com.ruoyi.system.api.RemotePublicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.modularbusiness.mapper.AubitFieldbodyMapper;
import com.ruoyi.modularbusiness.domain.AubitFieldbody;
import com.ruoyi.modularbusiness.service.IAubitFieldbodyService;

/**
 * 签核内容Service业务层处理
 * 
 * @author liuzhen
 * @date 2023-03-04
 */
@Service
public class AubitFieldbodyServiceImpl implements IAubitFieldbodyService 
{
    @Autowired
    private AubitFieldbodyMapper aubitFieldbodyMapper;

    @Autowired
    private FormrecordingMapper formrecordingMapper;

    @Autowired
    private IFormrecordingService iFormrecordingService;

    @Autowired
    private RemotePublicService remotePublicService;


    @Override
    public Long batchSave(List<AubitFieldbody> aubitFieldbody) {
        return aubitFieldbodyMapper.batchSave(aubitFieldbody);
    }

    @Override
    public List<AubitFieldbody> selectAubitFieldbody_topid(String code) {
        return aubitFieldbodyMapper.selectAubitFieldbody_topid(code);
    }

    /**
     * 查询签核内容
     * 
     * @param afbdId 签核内容主键
     * @return 签核内容
     */
    @Override
    public AubitFieldbody selectAubitFieldbodyByAfbdId(Long afbdId)
    {
        return aubitFieldbodyMapper.selectAubitFieldbodyByAfbdId(afbdId);
    }

    /**
     * 查询签核内容列表
     * 
     * @param aubitFieldbody 签核内容
     * @return 签核内容
     */
    @Override
    public List<AubitFieldbody> selectAubitFieldbodyList(AubitFieldbody aubitFieldbody)
    {
        return aubitFieldbodyMapper.selectAubitFieldbodyList(aubitFieldbody);
    }

    /**
     * 新增签核内容
     * 
     * @param aubitFieldbody 签核内容
     * @return 结果
     */
    @Override
    public int insertAubitFieldbody(AubitFieldbody aubitFieldbody)
    {
        aubitFieldbody.setCreateTime(DateUtils.getNowDate());
        return aubitFieldbodyMapper.insertAubitFieldbody(aubitFieldbody);
    }

    /**
     * 修改签核内容
     * 
     * @param aubitFieldbody 签核内容
     * @return 结果
     */
    @Override
    public int updateAubitFieldbody(AubitFieldbody aubitFieldbody)
    {
        aubitFieldbody.setUpdateTime(DateUtils.getNowDate());
        aubitFieldbody.setAuditTime(DateUtils.getTime());
        int res=aubitFieldbodyMapper.updateAubitFieldbody(aubitFieldbody);
        Formrecording formrecording=new Formrecording();
        formrecording.setFrid(Long.parseLong(aubitFieldbody.getTopid().toString()));
        List<FormRtbVo> flist=formrecordingMapper.seluserform_all_state(formrecording);
//        FormRtbVo formRtbVo=(FormRtbVo)flist.get(0);
        String str=flist.get(0).getAudits();
        String[] strArr= str.split(",");
        String st="2";
        for (int i = 0; i < strArr.length; i++) {
            if(strArr[i].equals("1")){
                st="1";
            }
            if(strArr[i].equals("3")){
                st="3";
                break;
            }
        }
        Formrecording f1=new Formrecording();
        f1.setFrid(Long.parseLong(aubitFieldbody.getTopid().toString()));
        f1.setStates(st);
        formrecordingMapper.updateFormrecording(f1);
//        System.out.println("flist==========================>"+flist);
        List<AubitTypeVo> list=iFormrecordingService.get_trid_notice(aubitFieldbody.getTopid());
        if(list.size()>0&&aubitFieldbody.getAfbdState().equals("2")){
            String urls="pages/login/login?url=pages/formlist/formbody/formbody!name,"+list.get(0).getName()+
                    "-%frid,"+aubitFieldbody.getTopid()+"-%abbreviated,"+list.get(0).getAbbreviated()+"-%fbid,"+
                    list.get(0).getAfbd_id()+"-%type,3-%times,"+list.get(0).getCreate_time();
            JSONObject jo=new JSONObject();
            jo.put("url",urls);
            jo.put("username",list.get(0).getUsername());
            jo.put("to",list.get(0).getQywx());
            jo.put("deptname",list.get(0).getDept_name());
            jo.put("type",list.get(0).getName());
            jo.put("creatime",list.get(0).getCreate_time());
            remotePublicService.qywxxcx_msg_push(jo, SecurityConstants.INNER);
        }
        return res;
//        return 0;
    }

    /**
     * 批量删除签核内容
     * 
     * @param afbdIds 需要删除的签核内容主键
     * @return 结果
     */
    @Override
    public int deleteAubitFieldbodyByAfbdIds(Long[] afbdIds)
    {
        return aubitFieldbodyMapper.deleteAubitFieldbodyByAfbdIds(afbdIds);
    }

    /**
     * 删除签核内容信息
     * 
     * @param afbdId 签核内容主键
     * @return 结果
     */
    @Override
    public int deleteAubitFieldbodyByAfbdId(Long afbdId)
    {
        return aubitFieldbodyMapper.deleteAubitFieldbodyByAfbdId(afbdId);
    }
}
